<?php

namespace app\admin\controller;


use app\admin\model\Price;
use cmf\controller\AdminBaseController;
use think\console\Input;
use think\facade\Db;


class PriceController extends AdminBaseController
{
    /**
     *列表
     * @adminMenu(
     *     'name'   => '价格维护',
     *     'parent' => 'admin/coil/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        //搜索部分
//        $key_status = input('get.key_status/d');
        $ses_id = input('id');
        $this->assign('ses_id', $ses_id);
//        var_dump($ses_id);
        $lists = Price::order('id desc')->where('ses_id',$ses_id);
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }

        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);

        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(), 'domain' => request()->domain() . '/upload/']);
    }

    public function add()
    {
        $ses_id = input('ses_id');
        $this->assign('ses_id', $ses_id);
        return $this->fetch('', ['domain' => request()->domain() . '/upload/']);
    }

    public function addPost()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        if (!$params) {
            $this->error('非法参数');
        }

        Price::create($params);

        $this->success('操作成功');
    }

    public function edit()
    {
        $id = input('get.id/d');

        if (!is_numeric($id)) {
            $this->error('非法参数');
        }

        $res = Price::where('id', $id)->find();


        return $this->fetch('', ['vo' => $res, 'domain' => request()->domain() . '/upload/']);
    }

    public function editPost()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        if (!$params || empty($params['id'])) {
            $this->error('非法参数');
        }

        Price::update($params);

        $this->success('操作成功');
    }

    public function del()
    {
        $id = input('get.id/d');

        if (!(is_numeric($id) && $id > 0)) {
            $this->error('非法参数');
        }

        Price::where('id', $id)->delete();

        $this->success('删除成功');
    }

    public function field()
    {
        $id = input('get.id/d');
        $field = input('get.field');

        if (!(is_numeric($id) && $id > 0 && $field)) {
            $this->error('非法参数');
        }

        $info = Price::field($field)->where('id', $id)->find();

        $this->success('ok', '', $info[$field]);
    }
}